<?php

class CMS_Db_Table_Abstract_Xml extends Zend_Db_Table_Abstract
{
	protected $sqlOrderByInstruction = null;
	protected $cleanNS = null;
	protected $tata = "tata";
    
	
	public function toXml($rootName = "root", $sqlOrderBy = "", $cleanNS = ""){
		
		$this->sqlOrderByInstruction = $sqlOrderBy;
		$this->cleanNS = $cleanNS;
		$this->xmlWriter = new XMLWriter();
		$this->xmlWriter->openMemory();
		$this->xmlWriter->setIndent(true);
		$this->xmlWriter->startDocument('1.0', 'UTF-8');
		
		// open root
		$this->xmlWriter->startElement($rootName);
		
		// recursive build of the tree
		$this->_build();
		
		// close root
		$this->xmlWriter->endElement();
		
		// return xml as string
		return $this->xmlWriter->outputMemory();
	}
	
	protected function _build ($param = ""){
		$sql = $this->select()->order($this->sqlOrderByInstruction);
		$res = $this->fetchAll($sql);
		foreach($res->toArray() as $file){
		$this->xmlWriter->startElement('row');
			foreach($file as $prop=>$val){
				$p = $this->cleanNameSpace($prop);
				@$this->xmlWriter->writeAttribute($p, $val);
			}	
			$this->xmlWriter->endElement();
		}
	}
	
	protected function cleanNameSpace($str){
		return preg_replace('`'.$this->cleanNS.'`','',$str);
	}
	
	
}
